$(function () {
	let i = 0;
	let strlist = "", beforedate = null;
	let str = '';
	function addZero(n) {
		return n < 10 ? '0' + n : n
	}
	// 1.处理导航栏的日期
	function changeDate() {
		let date = new Date();
		let montharr = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
		let month = date.getMonth();
		let day = date.getDate();
		// console.log(month, day);
		let hour = date.getHours();
		// console.log(hour);
		$('.hleft strong').html(addZero(day))
		$('.hleft span').html(montharr[month])
		if (hour >= 12) {
			$('.hleft h3').html('知乎日报')
		} else {
			$('.hleft h3').html('早上好')
		}
	}
	changeDate()

	// 2.轮播图的
	// @1 获取数据
	$.get('http://localhost:8888/api/swiper', function (data) {
		render(data)
	})
	// @2 循环渲染
	function render(data) {
		data.forEach((item, index) => {
			let { author, id, img, title } = item;
			str += `
			<div class="swiper-slide" id=${id}>
						<img src="${img}" alt="">
						<div class="title">
							<h1>${title}</h1>
							<p>${author}</p>
						</div>
					</div>
			`
		})
		// console.log(str);
		$('.swiper-wrapper').html(str);
		startswiper()
	}
	// @3 渲完启动轮播图
	function startswiper() {
		var swiper = new Swiper('.swiper-container', {
			autoplay: {
				delay: 1000,
				stopOnLastSlide: false,
				disableOnInteraction: true,
			},
			loop: true,
			pagination: {
				el: '.swiper-pagination',
			},
		});
	}

	//3. 获取列表数据
	//@1 获取数据
	function getlistData() {
		if (i > 5) {
			$('.loadmore').html('最新数据已经加载完了哦^_^')
			return;
		}
		let date = new Date();
		let year = date.getFullYear();
		let month = addZero(date.getMonth() + 1);
		let day = date.getDate();
		let datevalue = `${year}-${month}-${day}`
		// console.log(datevalue);
		$.get('http://localhost:8888/api/articleList?date=2021-05-19', function (data) {
			// console.log(data);
			listrender(data);
		})
	}
	getlistData()
	//设置日期是否显示
	function setdate() {
		let date = new Date().getTime() - (24 * 60 * 60 * 1000) * i;
		beforedate = {
			month: (new Date(date).getMonth()) + 1,
			day: addZero(new Date(date).getDate())
		}
		i++;
	}
	//@2 获取数据
	function listrender(data) {
		setdate();
		if (i > 1) {
			strlist += `<p class="listhr">${beforedate.month}月${beforedate.day}日<b></b></p>`;
		}
		data.forEach((item) => {
			let { author, title, img, id, des } = item;
			strlist += `<div class="item" data-id="${id}">
                      <div class="item_left">
                          <h3>${title}</h3>
                          <p>${author}·${des}分钟阅读</p>
                      </div>
                      <img src="${img}" alt="">
                  </div>`
		})
		$(".list").html(strlist);
		loadmore();
	}


	// 加载更多
	function loadmore() {
		let ob = new IntersectionObserver((changes) => {
			if (changes[0].isIntersecting) {
				getlistData();
			}
		})
		ob.observe($('.loadmore')[0])
	}

	//跳转详情页   
	$(".list").delegate(".item", "click", function () {
		let aid = $(this).attr("data-id");
		// console.log(aid);
		location.href = "pages/detials.html?id=" + aid;
	});
})